package com.ruoyi.campus.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.campus.domain.TreeSelects;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.campus.mapper.MyCommodityTypeMapper;
import com.ruoyi.campus.domain.MyCommodityType;
import com.ruoyi.campus.service.IMyCommodityTypeService;

/**
 * 类别Service业务层处理
 *
 * @author zht
 * @date 2021-12-09
 */
@Service
public class MyCommodityTypeServiceImpl implements IMyCommodityTypeService {
    @Autowired
    private MyCommodityTypeMapper myCommodityTypeMapper;

    /**
     * 查询类别
     *
     * @param typeId 类别主键
     * @return 类别
     */
    @Override
    public MyCommodityType selectMyCommodityTypeByTypeId(Long typeId) {
        return myCommodityTypeMapper.selectMyCommodityTypeByTypeId(typeId);
    }

    /**
     * 构建前端所需要的下拉树结构
     *
     * @param types 分类列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelects> buildTypeTreeSelect(List<MyCommodityType> types) {
        List<MyCommodityType> typeTrees = buildTypeTree(types);
        return typeTrees.stream().map(TreeSelects::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要的树结构
     *
     * @param types 分类列表
     * @return 树结构列表
     */
    public List<MyCommodityType> buildTypeTree(List<MyCommodityType> types) {
        List<MyCommodityType> returnList = new ArrayList<MyCommodityType>();
        List<Long> tempList = new ArrayList<Long>();
        for (MyCommodityType type : types) {
            tempList.add(type.getTypeId());
        }
        for (Iterator<MyCommodityType> iterator = types.iterator(); iterator.hasNext(); ) {
            MyCommodityType type = (MyCommodityType) iterator.next();
            // 如果是顶级节点，遍历该父节点的所有子节点
            if (!tempList.contains(type.getParentId())) {
                recursionFn(types, type);
                returnList.add(type);
            }
        }
        if (returnList.isEmpty()) {
            returnList = types;
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @param list 类别列表
     * @param type 类别
     */
    private void recursionFn(List<MyCommodityType> list, MyCommodityType type) {
        // 得到子节点列表
        List<MyCommodityType> childList = getChildList(list, type);
        type.setChildren(childList);
        for (MyCommodityType tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<MyCommodityType> list, MyCommodityType tChild) {
        return getChildList(list, tChild).size() > 0 ? true : false;
    }

    /**
     * 得到子节点列表
     */
    private List<MyCommodityType> getChildList(List<MyCommodityType> list, MyCommodityType type) {
        List<MyCommodityType> tlist = new ArrayList<MyCommodityType>();
        Iterator<MyCommodityType> it = list.iterator();
        while (it.hasNext()) {
            MyCommodityType n = (MyCommodityType) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == type.getTypeId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }


    /**
     * 查询类别列表
     *
     * @param myCommodityType 类别
     * @return 类别
     */
    @Override
    public List<MyCommodityType> selectMyCommodityTypeList(MyCommodityType myCommodityType) {
        return myCommodityTypeMapper.selectMyCommodityTypeList(myCommodityType);
    }

    /**
     * 新增类别
     *
     * @param myCommodityType 类别
     * @return 结果
     */
    @Override
    public int insertMyCommodityType(MyCommodityType myCommodityType) {
        myCommodityType.setCreateTime(DateUtils.getNowDate());
        MyCommodityType info = myCommodityTypeMapper.selectMyCommodityTypeByTypeId(myCommodityType.getParentId());
        myCommodityType.setAncestors(info.getAncestors() + "," + myCommodityType.getParentId());
        return myCommodityTypeMapper.insertMyCommodityType(myCommodityType);
    }

    /**
     * 校验类别名称是否唯一
     *
     * @param myCommodityType 类别
     * @return 结果
     */
    @Override
    public String checkTypeNameUnique(MyCommodityType myCommodityType) {
        Long typeId = StringUtils.isNull(myCommodityType.getTypeId()) ? -1L : myCommodityType.getTypeId();
        MyCommodityType info = myCommodityTypeMapper.checkTypeNameUnique(myCommodityType.getTypeName(), myCommodityType.getParentId());
        if (StringUtils.isNotNull(info) && info.getTypeId().longValue() != typeId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 修改保存类别信息
     *
     * @param myCommodityType 类别
     * @return 结果
     */
    @Override
    public int updateMyCommodityType(MyCommodityType myCommodityType) {
        myCommodityType.setUpdateTime(DateUtils.getNowDate());
        MyCommodityType newParentType = myCommodityTypeMapper.selectMyCommodityTypeByTypeId(myCommodityType.getParentId());
        MyCommodityType oldType = myCommodityTypeMapper.selectMyCommodityTypeByTypeId(myCommodityType.getTypeId());
        if (StringUtils.isNotNull(newParentType) && StringUtils.isNotNull(oldType)){
            String newAncestors = newParentType.getAncestors() + "," + newParentType.getTypeId();
            String oldAncestors = oldType.getAncestors();
            myCommodityType.setAncestors(newAncestors);
            updateTypeChildren(myCommodityType.getTypeId(),newAncestors,oldAncestors);
        }
        return myCommodityTypeMapper.updateMyCommodityType(myCommodityType);
    }

    /**
     * 修改子元素关系
     *
     * @param typeId 被修改的类别ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    private void updateTypeChildren(Long typeId, String newAncestors, String oldAncestors) {
        List<MyCommodityType> children = myCommodityTypeMapper.selectChildrenTypeById(typeId);
        for (MyCommodityType child : children) {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors,newAncestors));
        }
        if (children.size() > 0){
            myCommodityTypeMapper.updateTypeChildren(children);
        }
    }

    /**
     * 批量删除类别
     *
     * @param typeIds 需要删除的类别主键
     * @return 结果
     */
    @Override
    public int deleteMyCommodityTypeByTypeIds(Long[] typeIds) {
        return myCommodityTypeMapper.deleteMyCommodityTypeByTypeIds(typeIds);
    }

    /**
     * 删除类别信息
     *
     * @param typeId 类别主键
     * @return 结果
     */
    @Override
    public int deleteMyCommodityTypeByTypeId(Long typeId) {
        return myCommodityTypeMapper.deleteMyCommodityTypeByTypeId(typeId);
    }

    /**
     * 是否存在类别子节点
     * @param typeId 类别Id
     * @return 结果
     */
    @Override
    public boolean hasChildByTypeId(Long typeId) {
        int result = myCommodityTypeMapper.hasChildByTypeId(typeId);
        return result > 0 ? true : false;
    }

    /**
     * 查询是否存在商品引用
     * @param typeId 类别Id
     * @return 结果
     */
    @Override
    public boolean checkTypeExistComm(Long typeId) {
        int result = myCommodityTypeMapper.hasCommByTypeId(typeId);
        return result > 0 ? true : false;
    }
}
